package simulations.factory;

import simulations.Entity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Factory for creating Entities from a model file
 * @author Zach Michaelov
 */
public class EntityFactory {

    public static Entity fromFile(File modelFile) {
        Entity entity = new Entity();
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    if (type.equals("spring") || type.equals("muscle")) {
                        entity.add(SpringFactory.fromScanner(line, entity, type));
                    }
                    else if (type.equals("mass")) {
                        entity.add(MassFactory.fromScanner(line, entity));
                    }
                }
            }
            input.close();
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            e.printStackTrace();
        }
        return entity;
    }
}
